<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>广告运营管理</el-breadcrumb-item>
        <el-breadcrumb-item>广告解锁功能</el-breadcrumb-item>
        <el-breadcrumb-item>渠道免广告</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
        ><!-- 自定义操作栏 -->
        <template slot-scope="scope" slot="menu">
          <br />
          <el-button
            v-if="scope.row.state == 0"
            icon="el-icon-open"
            type="text"
            size="small"
            @click.stop="openAndClose(scope.row, 1)"
            >开启
          </el-button>
          <el-button
            v-if="scope.row.state == 1"
            icon="el-icon-open"
            type="text"
            size="small"
            @click.stop="openAndClose(scope.row, 0)"
            >关闭
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {
  getAppConfigChannelAdList,
  updateAppConfigChannelAd,
  addAppConfigChannelAd,
  closeAndOpenConfigChannelAd,
} from "@/api/cat/ad";
import { catUrl } from "@/config/env";
import { catversion } from "@/api/cat/baseUrls";

export default {
  data() {
    return {
      page: {
        total: 0,
      },
      type: true,
      data: [],
      form: {},
      option: {
        index: true,
        indexLabel: "序号",
        addBtn: true,
        editBtn: true,
        viewBtn: false,
        delBtn: false,
        searchBtn: true,
        refreshBtn: true,
        excelBtn: true,
        border: true,
        column: [
          {
            label: "渠道号",
            prop: "channel_id",
            type: "input",
            search: true,
            rules: [
              {
                required: true,
                message: "渠道号必须填写",
              },
            ],
            pattern: "\\.+",
          },
          {
            label: "版本号",
            prop: "version_name",
            type: "select",
            search: true,
            placeholder: "请输入版本号",
            dicUrl: `${catUrl}/${catversion.baseUrl}`,
            props: {
              label: "app_version",
              value: "app_version",
            },
            dicFormatter: (res) => {
              return res.source;
            },
            rules: [
              {
                required: true,
                message: "版本号必须填写",
              },
            ],
          },
          {
            label: "状态",
            prop: "state",
            type: "select",
            search: true,
            dicData: [
              {
                label: "开启",
                value: 1,
              },
              {
                label: "关闭",
                value: 0,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
            rules: [
              {
                required: true,
                message: "状态必须填写",
              },
            ],
            pattern: "\\.+",
          },
          {
            label: "创建时间",
            prop: "createtime",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
        ],
      },
    };
  },
  methods: {
    /**修改开启和关闭配置 */
    async openAndClose(value, type) {
      let { data } = await closeAndOpenConfigChannelAd(value.id, type);
      this.getList();
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //封装参数
      let param = Object.assign(
        {
          pageNum: this.page.currentPage,
          pageSize: this.page.pageSize,
        },
        this.params
      );
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await getAppConfigChannelAdList(param);
      //修改分页内容
	  console.log(response)
      this.pages(response.data.source);
      //数据赋值
      let list = response.data.source.records;
      this.data = list;
      this.loading = false;
    },

    /**修改 */
    async rowUpdate(row, index, done, loading) {
      let data = await updateAppConfigChannelAd(row);
      this.getList();
      done();
    },
    /**保存 */
    async rowSave(row, done) {
      delete row["id"];
      let data = await addAppConfigChannelAd(row);
      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    pages(data) {
      this.page.total = data.total;
      this.page.pageSize = data.size;
      this.page.currentPage = data.current;
    },
    searchChange(params, done) {
      this.condition = [];
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>